home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / asyam.zip / ISR.C < prev    next >
C/C++ Source or Header  |  1993-06-24  |  7KB  |  290 lines

  1. #include <dos.h>
  2.  
  3. #include "async.h"
  4.  
  5. extern  int UART_ports[];
  6. extern  int UART_interrupts[];
  7. extern  int UART_onmask[];
  8. extern  int UART_offmask[];
  9.  
  10. extern  struct async_portS async_port[4];
  11. extern  int MSRStatus[NUMBEROFPORTS];
  12. extern  int LSRStatus[NUMBEROFPORTS];
  13.  
  14. // Prototypes for ISR's
  15. void    interrupt UART_ISR1(void);
  16. void    interrupt UART_ISR2(void);
  17. void    interrupt UART_ISR3(void);
  18. void    interrupt UART_ISR4(void);
  19.  
  20. // Prototypes for ISR utilities.
  21. static  void near ISRmodem(int comport);
  22. static  void near ISRtx(int comport);
  23. static  void near ISRrx(int comport);
  24. static  void near ISRstatus(int comport);
  25.  
  26. void    init_ISR(int comport)
  27. {
  28.     int     icu, mcr;
  29.  
  30.     asm cli;
  31.  
  32.     // Get old interrupt vector
  33.     async_port[comport].oldISR = getvect(UART_interrupts[comport]);
  34.  
  35.     // Set new interrupt vector
  36.     switch (comport) {
  37.         case COM1:
  38.             setvect(UART_interrupts[comport], UART_ISR1);
  39.             break;
  40.         case COM2:
  41.             setvect(UART_interrupts[comport], UART_ISR2);
  42.             break;
  43.         case COM3:
  44.             setvect(UART_interrupts[comport], UART_ISR3);
  45.             break;
  46.         case COM4:
  47.             setvect(UART_interrupts[comport], UART_ISR4);
  48.             break;
  49.     }
  50.  
  51.     // Set OUT2 bit to enable interrupts
  52.     mcr = inportb(UART_ports[comport]+MCR);
  53.     mcr |= MCR_OUT2;
  54.     outportb(UART_ports[comport]+MCR, mcr);
  55.  
  56.     // Enable interrupts we want
  57.     outportb(UART_ports[comport]+IER, 0x08);
  58.  
  59.     // Unmask the interrupts in the ICU
  60.     icu = inportb(0x21);
  61.     icu &= UART_onmask[comport];
  62.     outportb(0x21, icu);
  63.  
  64.     asm sti;
  65. }
  66.  
  67. void    deinit_ISR(int comport)
  68. {
  69.     int     icu;
  70.  
  71.     // Return the old interrupt vector to normal
  72.     setvect(UART_interrupts[comport], async_port[comport].oldISR);
  73.  
  74.     // Remask the interrupts in the ICU
  75.     icu = inportb(0x21);
  76.     icu |= UART_offmask[comport];
  77.     outportb(0x21, icu);
  78. }
  79.  
  80. // Interrupt Service Routines
  81.  
  82. void    interrupt far UART_ISR1()                   // ISR for COM1
  83. {
  84.     int     iir;
  85.     int     done = 0;
  86.  
  87.     while (!done) {
  88.         iir = inportb(UART_ports[COM1]+IIR);
  89.  
  90.         if (iir&1) {
  91.             done = 1;
  92.         } else {
  93.             iir&=6;
  94.             iir>>=1;
  95.  
  96.             switch (iir) {
  97.                 case 0:
  98.                     ISRmodem(COM1);
  99.                     break;
  100.                 case 1:
  101.                     ISRtx(COM1);
  102.                     break;
  103.                 case 2:
  104.                     ISRrx(COM1);
  105.                     break;
  106.                 case 3:
  107.                     ISRstatus(COM1);
  108.                     break;
  109.             }
  110.         }
  111.     }
  112.  
  113.     outport(0x20, 0x20);
  114. }
  115.  
  116. void    interrupt far UART_ISR2()                   // ISR for COM2
  117. {
  118.     int     iir;
  119.     int     done = 0;
  120.  
  121.     while (!done) {
  122.         iir = inportb(UART_ports[COM2]+IIR);
  123.  
  124.         if (iir&1) {
  125.             done = 1;
  126.         } else {
  127.             iir&=6;
  128.             iir>>=1;
  129.  
  130.             switch (iir) {
  131.                 case 0:
  132.                     ISRmodem(COM2);
  133.                     break;
  134.                 case 1:
  135.                     ISRtx(COM2);
  136.                     break;
  137.                 case 2:
  138.                     ISRrx(COM2);
  139.                     break;
  140.                 case 3:
  141.                     ISRstatus(COM2);
  142.                     break;
  143.             }
  144.         }
  145.     }
  146.  
  147.     outport(0x20, 0x20);
  148. }
  149.  
  150. void    interrupt far UART_ISR3()                   // ISR for COM3
  151. {
  152.     int     iir;
  153.     int     done = 0;
  154.  
  155.     while (!done) {
  156.         iir = inportb(UART_ports[COM3]+IIR);
  157.  
  158.         if (iir&1) {
  159.             done = 1;
  160.         } else {
  161.             iir&=6;
  162.             iir>>=1;
  163.  
  164.             switch (iir) {
  165.                 case 0:
  166.                     ISRmodem(COM3);
  167.                     break;
  168.                 case 1:
  169.                     ISRtx(COM3);
  170.                     break;
  171.                 case 2:
  172.                     ISRrx(COM3);
  173.                     break;
  174.                 case 3:
  175.                     ISRstatus(COM3);
  176.                     break;
  177.             }
  178.         }
  179.     }
  180.  
  181.     outport(0x20, 0x20);
  182. }
  183.  
  184. void    interrupt far UART_ISR4()                   // ISR for COM4
  185. {
  186.     int     iir;
  187.     int     done = 0;
  188.  
  189.     while (!done) {
  190.         iir = inportb(UART_ports[COM4]+IIR);
  191.  
  192.         if (iir&1) {
  193.             done = 1;
  194.         } else {
  195.             iir&=6;
  196.             iir>>=1;
  197.  
  198.             switch (iir) {
  199.                 case 0:
  200.                     ISRmodem(COM4);
  201.                     break;
  202.                 case 1:
  203.                     ISRtx(COM4);
  204.                     break;
  205.                 case 2:
  206.                     ISRrx(COM4);
  207.                     break;
  208.                 case 3:
  209.                     ISRstatus(COM4);
  210.                     break;
  211.             }
  212.         }
  213.     }
  214.  
  215.     outport(0x20, 0x20);
  216. }
  217.  
  218. void    near ISRmodem(int comport)
  219. {
  220.     MSRStatus[comport] = inportb(UART_ports[comport]+MSR);
  221. }
  222.  
  223. void    near ISRtx(int comport)
  224. {
  225.     int     tst;
  226.  
  227.     // Read from buffer and transmit -- Only does one character right now, no fifo support YET.
  228.     while (1) {
  229.         if (async_port[comport].txbuflength) {
  230.  
  231.             // There is something waiting to be sent, send it
  232.             outportb(UART_ports[comport], async_port[comport].txbuf[async_port[comport].txtail]);
  233.  
  234.             // Move buffer tail
  235.             async_port[comport].txtail++;
  236.             async_port[comport].txbuflength--;
  237.             if (async_port[comport].txtail == TXBUFSIZE) {
  238.                 async_port[comport].txtail = 0;
  239.             }
  240.  
  241.             tst = inport(UART_ports[comport]+LSR);
  242.             if (tst != 0x20) {
  243.                 break;
  244.             }
  245.  
  246.         } else {
  247.             // There is nothing, so inhibit TX interrupts
  248.             tst = inportb(UART_ports[comport]+IER);
  249.             tst &= 13;
  250.             outportb(UART_ports[comport]+IER, tst);
  251.             break;
  252.         }
  253.     }
  254. }
  255.  
  256. void    near ISRrx(int comport)
  257. {
  258.     int     rx, tst;
  259.     int     done = 0;
  260.  
  261.     while (!done) {
  262.         rx = inportb(UART_ports[comport]);
  263.  
  264.         // Place character into buffer
  265.         async_port[comport].rxbuf[async_port[comport].rxhead] = rx;
  266.  
  267.         // Increment rx buffer head
  268.         async_port[comport].rxhead++;
  269.         async_port[comport].rxbuflength++;
  270.         if (async_port[comport].rxhead == RXBUFSIZE) {
  271.             async_port[comport].rxhead = 0;
  272.         }
  273.  
  274.         tst = inportb(UART_ports[comport]+LSR);
  275.  
  276.         if (!(tst & 0x01)) {                    // Check for more in receive
  277.             done = 1;
  278.         }
  279.  
  280.         if (tst & 0x40) {                       // Check transmit shift register
  281.             ISRtx(comport);
  282.         }
  283.     }
  284. }
  285.  
  286. void    near ISRstatus(int comport)
  287. {
  288.     LSRStatus[comport] = inport(UART_ports[comport]+LSR);
  289. }
  290.